package com.ck.shardingspheremaster.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 描述:
 * 路由到表的精确分片算法
 *
 * @author yd
 * @create 2019-05-25 10:57
 */
@Slf4j
public final class PreciseModuloShardingTableAlgorithm implements PreciseShardingAlgorithm<Integer> {

    @Override
    public String doSharding(final Collection<String> tableNames, final PreciseShardingValue<Integer> shardingValue) {
        log.info("【精确分表算法】：[tableNames]:{},[shardingValue]:{}",tableNames,shardingValue);
        for (String each : tableNames) {
            if (each.endsWith(shardingValue.getValue() % 2 + "")) {
                log.info("【路由结果】：{}", each);
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }
}